package cn.fl.ref.fnccappayeeinfo.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fnccappayeeinfo.service.IFncCapPayeeInfoService;
import cn.fl.ref.fnccappayeeinfo.transform.FncCapPayeeInfoVOTransform;
import cn.fl.ref.fnccappayeeinfo.vo.FncCapPayeeInfoVO;
import cn.fl.ref.fncprjeaplysettle.service.IFncPrjEaplySettleService;
import cn.fl.ref.fncprjeaplysettle.vo.FncPrjEaplySettleVO;
import cn.fl.ref.fncprjpaysch.service.IFncPrjPaySchDJrnlService;
import cn.fl.ref.fncprjpaysch.service.IFncPrjPaySchService;
import cn.fl.ref.fncprjpaysch.vo.FncPrjPaySchDJrnlVO;
import cn.fl.ref.fncprjpaysch.vo.FncPrjPaySchMVO;
import cn.fl.system.dto.SysBkAccInfoDTO;
import cn.fl.system.service.ISysBkInfoApiService;
import cn.fl.system.service.ISysBkInfoService;
import cn.fl.system.vo.SysBkInfoVO;
import fl.flow.facade.flow.api.IFlowApiService;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.dto.SysFlowSHDTO;
import fl.ref.crbaseinfo.constant.CrBaseInfoConstant;
import fl.ref.fnccappayeeinfo.condition.FncCapPayeeInfoQueryCondition;
import fl.ref.fnccappayeeinfo.constant.FncCapPayeeInfoConstant;
import fl.ref.fnccappayeeinfo.dto.FncCapPayeeInfoDTO;
import fl.ref.fnccappayeeinfo.service.IFncCapPayeeInfoApiService;
import fl.ref.fncprjbscinfo.dto.FncPrjBscInfoDTO;
import fl.ref.fncprjbscinfo.service.IFncPrjBscInfoApiService;
import fl.ref.fncprjeaplysettle.constant.FncPrjEaplySettleConstant;
import fl.ref.fncprjfstpyinfo.dto.FncPrjDetailDTO;
import fl.ref.fncprjfstpyinfo.service.IFncPrjFstpyInfoApiService;
import fl.ref.fncprjpaysch.condition.FncPrjPaySchDJrnlCondition;
import fl.ref.fncprjpaysch.constant.FncPrjPaySchConstant;
import fl.ref.fribaseinfo.dto.BankAccInfoDTO;
import fl.ref.fribaseinfo.service.IBankAccInfoApiService;

import fl.ref.fribaseinfo.service.IFriBaseInfoApiService;

/**
 * @Description: 再融资付款收款方信息表
 * @Description:业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 * 服务实现层
 */
@Service
public class FncCapPayeeInfoServiceImpl implements IFncCapPayeeInfoService {


	private static final Logger logger = LoggerFactory.getLogger(IFncCapPayeeInfoService.class);

	@Autowired
	private IFncCapPayeeInfoApiService  fncCapPayeeInfoApiService;
	@Autowired
	private IFncPrjBscInfoApiService fncPrjBscInfoApiService;
	@Autowired
	private IFncPrjFstpyInfoApiService fncPrjFstpyInfoApiService;
	@Autowired
	private ISysBkInfoService sysBkInfoService;

	@Autowired
	private IFncPrjPaySchService fncPrjPaySchService;
	@Autowired
	private IFncPrjEaplySettleService fncPrjEaplySettleService;

	/**
	 * 融资机构\银行帐号信息
	 */
	@Autowired
	private IBankAccInfoApiService bankAccInfoApiService;

	@Autowired
	private IFncPrjPaySchDJrnlService fncPrjPaySchDJrnlService;

	/**
	 * 字典服务层BEAN
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/**
	 * 狮桥支付银行账户
	 */
	@Autowired
	private ISysBkInfoApiService sysBkInfoApiService;

	/**
	 * 流程服务
	 */
	@Autowired
	private IFlowApiService flowApiService;



	@Override
	public FncCapPayeeInfoVO selectById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		FncCapPayeeInfoDTO dto = this.fncCapPayeeInfoApiService.selectById(id);
		return FncCapPayeeInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(FncCapPayeeInfoVO entity) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		//系统日志-有用户的情况下
		BaseLogUtils.info(logger, "add", "新增FncCapPayeeInfo数据.", CurrentThreadContext.getCurrentUserName());

		//系统日志-无用户的情况下
		BaseLogUtils.info(logger, "add", "新增FncCapPayeeInfo数据.");
		return this.fncCapPayeeInfoApiService.add(FncCapPayeeInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.fncCapPayeeInfoApiService.removeById(id);
	}

	@Override
	public boolean modify(FncCapPayeeInfoVO entity) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.fncCapPayeeInfoApiService.modify(FncCapPayeeInfoVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		if(null == page)page = new Pager();
		return this.fncCapPayeeInfoApiService.searchListPage(page);
	}


	@Override
	public List<FncCapPayeeInfoVO> searchList(FncCapPayeeInfoQueryCondition condition) {
		// TODO Auto-generated method stub
		return FncCapPayeeInfoVOTransform.toVOList(this.fncCapPayeeInfoApiService.searchList(condition))  ;
	}
	/*
	 * 首付款录入付款信息
	 * @see cn.fl.ref.fnccappayeeinfo.service.IFncCapPayeeInfoService#addCapFirstpay(java.lang.Long)
	 */
	@Override
	public void addCapFirstpay(Long prjId) throws BaseException {

		FncPrjDetailDTO fncPrjDetailDTO = this.fncPrjFstpyInfoApiService.selectByFncPrjId(prjId);
		FncPrjBscInfoDTO fpbdto=this.fncPrjBscInfoApiService.selectById(prjId);
		SysBkAccInfoDTO fkbkAcc = this.sysBkInfoApiService.selectSysBkAccInfoById(Long.parseLong(fncPrjDetailDTO.getFstpyPayAccno()),"11000","0000");
		BankAccInfoDTO skbkAcc = this.bankAccInfoApiService.selectById(Long.parseLong(fncPrjDetailDTO.getFstpyAccno()),"11000","0000");
		Map<String, String> crsbjlist=this.sysDictionaryApiService.searchGroupToMap("11000","0000",CrBaseInfoConstant.CR_BSC_INFO_CR_SBJ);
		SysBkInfoVO bkvo=sysBkInfoService.selectById(fkbkAcc.getBkId());
		SysFlowSHQueryCondition condition= new SysFlowSHQueryCondition();
		condition.setDataId(prjId);
		condition.setWordsInTheTable("FST");
		condition.setSysCd("11000");
		//condition.setBuOrgCd("0000");
		List<SysFlowSHDTO> list=this.flowApiService.searchFlowSHDTOList(condition);
		if(null!=list && list.size()==1){
			//this.fncCapPayeeInfoApiService.removeById(prjId);//by gengchunlan 20190410
			SysFlowSHDTO sfDTO = list.get(0);
			FncCapPayeeInfoDTO capee= new FncCapPayeeInfoDTO();

			capee.setId(fpbdto.getId());
			capee.setPayId(fpbdto.getId());
			capee.setPrjId(prjId);
			capee.setPayAmt(Math.round(fpbdto.getFstpyamt().doubleValue()));
			capee.setBkId(Long.parseLong(fpbdto.getFstpyaccno()));
			capee.setRcptUnt(skbkAcc.getAccNm());
			capee.setAccNo(skbkAcc.getAccNum());
			capee.setPayeeBkNm(skbkAcc.getBanAccNm());
			capee.setPayeeBkNo(skbkAcc.getBanAccNum());
			capee.setBkAccTypCd(skbkAcc.getPubVerPriCd());
			capee.setPurpCd("2");  // 1 利息 2本金  3 其他费用
			capee.setPayer(crsbjlist.get(fpbdto.getCrsbj()));//授信主体
			capee.setPayerAccNo(fkbkAcc.getAccNo());

			capee.setPayerBkNm(bkvo.getBkNm());
			capee.setPayerBkNo(bkvo.getBkNo());
			capee.setCrtUsrId(Long.parseLong(fpbdto.getCrtpsnecd()));
			capee.setCrtTm(new Date());
			capee.setPNo(sfDTO.getPNo());
			capee.setUseCd(fpbdto.getFncinsNM()+"首付款");
			capee.setWishPayday(new Date());

			capee.setIsQuick(FncCapPayeeInfoConstant.FNC_PAYEE_NOQUICK);
			capee.setCorpCode(FncCapPayeeInfoConstant.FNC_PAYEE_CROP_CODE_ZJB);
			capee.setPayWayCd(FncCapPayeeInfoConstant.FNC_PAYEE_PAYWAY_CD);
			capee.setPayeeTypCd(FncCapPayeeInfoConstant.FNC_PAYEE_PAYEETYP_CD);
			capee.setIsDel(0);
			capee.setFundTypCd(FncCapPayeeInfoConstant.FNC_PAYEE_FUNDTYPE_CD);
			this.fncCapPayeeInfoApiService.add(capee);
		}



	}

	/*
	 * 还款录入付款信息
	 *
	 */
	@Override
	public void addCappaysch(Long schmId) throws BaseException {

		FncPrjPaySchMVO mvo=this.fncPrjPaySchService.selectById(schmId);
		Long prjId=mvo.getProjId();
		FncPrjBscInfoDTO fpbdto=this.fncPrjBscInfoApiService.selectById(prjId);
		SysBkAccInfoDTO fkbkAcc = this.sysBkInfoApiService.selectSysBkAccInfoById(mvo.getPayAccId(),"11000","0000");
		BankAccInfoDTO skbkAcc = this.bankAccInfoApiService.selectById(mvo.getRcvpymtAccId(),"11000","0000");
		Map<String, String> crsbjlist=this.sysDictionaryApiService.searchGroupToMap("11000","0000",CrBaseInfoConstant.CR_BSC_INFO_CR_SBJ);
		SysBkInfoVO bkvo=sysBkInfoService.selectById(fkbkAcc.getBkId());
		SysFlowSHQueryCondition condition= new SysFlowSHQueryCondition();

		//condition.setDataId(schmId);
		//condition.setWordsInTheTable(FncPrjPaySchConstant.SCM_FLOW_RPAYS);
		//condition.setSysCd("11000");
		//List<SysFlowSHDTO> list=this.flowApiService.searchFlowSHDTOList(condition);
		//if(null!=list && list.size()==1){
		//this.fncCapPayeeInfoApiService.removeById(schmId);by gengchunlan 20190410
		//SysFlowSHDTO sfDTO = list.get(0);
		FncCapPayeeInfoDTO capee= new FncCapPayeeInfoDTO();

		capee.setPayId(schmId);
		capee.setPrjId(prjId);

		//收款方
		capee.setBkId(mvo.getRcvpymtAccId());
		capee.setRcptUnt(skbkAcc.getAccNm());
		capee.setAccNo(skbkAcc.getAccNum());
		capee.setPayeeBkNm(skbkAcc.getBanAccNm());
		capee.setPayeeBkNo(skbkAcc.getBanAccNum());
		capee.setBkAccTypCd(skbkAcc.getPubVerPriCd());

		//付款方
		capee.setPayer(crsbjlist.get(fpbdto.getCrsbj()));//授信主体
		capee.setPayerAccNo(fkbkAcc.getAccNo());
		capee.setPayerBkNm(bkvo.getBkNm());
		capee.setPayerBkNo(bkvo.getBkNo());

		capee.setCrtUsrId(Long.parseLong(fpbdto.getCrtpsnecd()));
		capee.setCrtTm(new Date());

		//capee.setPNo(sfDTO.getPNo());

		//capee.setWishPayday(mvo.getpay.getPayTm());

		capee.setIsQuick(FncCapPayeeInfoConstant.FNC_PAYEE_NOQUICK);
		capee.setCorpCode(FncCapPayeeInfoConstant.FNC_PAYEE_CROP_CODE_ZJB);
		capee.setPayWayCd(FncCapPayeeInfoConstant.FNC_PAYEE_PAYWAY_CD);
		capee.setPayeeTypCd(FncCapPayeeInfoConstant.FNC_PAYEE_PAYEETYP_CD);
		capee.setIsDel(0);
		capee.setFundTypCd(FncCapPayeeInfoConstant.FNC_PAYEE_FUNDTYPE_CD);


		FncPrjPaySchDJrnlCondition sdqc=new FncPrjPaySchDJrnlCondition();
		sdqc.setPaySchId(schmId);


		List<FncPrjPaySchDJrnlVO> sdvos=this.fncPrjPaySchDJrnlService.selectList(sdqc);
		for (FncPrjPaySchDJrnlVO sdvo : sdvos) {
			if(sdvo.getActualAmt().compareTo(new BigDecimal(0))==1){
				capee.setId(sdvo.getId());
				capee.setPayAmt(Math.round(sdvo.getActualAmt().doubleValue()));
				capee.setWishPayday(sdvo.getPayDt());
				if(sdvo.getFeeClsCd().equals("1")){
					capee.setPurpCd("2");  // 1 利息 2本金  3 其他费用
					capee.setUseCd(fpbdto.getFncinsNM()+"第"+sdvo.getPayPd()+"期还款本金");
				} else if(sdvo.getFeeClsCd().equals("2")){
					capee.setPurpCd("1");  // 1 利息 2本金  3 其他费用
					capee.setUseCd(fpbdto.getFncinsNM()+"第"+sdvo.getPayPd()+"期还款利息");
				}else if(sdvo.getFeeClsCd().equals("3")){
					capee.setPurpCd("3");  // 1 利息 2本金  3 其他费用
					capee.setUseCd(fpbdto.getFncinsNM()+"第"+sdvo.getPayPd()+"期还款超额抵押");
				}else if(sdvo.getFeeClsCd().equals("4")){
					capee.setPurpCd("3");  // 1 利息 2本金  3 其他费用
					capee.setUseCd(fpbdto.getFncinsNM()+"第"+sdvo.getPayPd()+"期还款融资端费用");
				}else if(sdvo.getFeeClsCd().equals("5")){
					capee.setPurpCd("3");  // 1 利息 2本金  3 其他费用
					capee.setUseCd(fpbdto.getFncinsNM()+"第"+sdvo.getPayPd()+"期还款其他费用");
				}


				this.fncCapPayeeInfoApiService.add(capee);
			}

		}

	}

	// TODO Auto-generated method stub

	//}
	/**
	 * 提前结清录入付款信息
	 *
	 */
	@Override
	public void addCapEaplySettle(Long settleId) throws BaseException {

		FncPrjEaplySettleVO esvo=this.fncPrjEaplySettleService.selectById(settleId);
		Long prjId=esvo.getPrjid();
		FncPrjBscInfoDTO fpbdto=this.fncPrjBscInfoApiService.selectById(prjId);

		SysBkAccInfoDTO fkbkAcc = this.sysBkInfoApiService.selectSysBkAccInfoById(esvo.getPayAcc(),"11000","0000");
		BankAccInfoDTO skbkAcc = this.bankAccInfoApiService.selectById(esvo.getRecvAcc(),"11000","0000");
		Map<String, String> crsbjlist=this.sysDictionaryApiService.searchGroupToMap("11000","0000",CrBaseInfoConstant.CR_BSC_INFO_CR_SBJ);
		SysBkInfoVO bkvo=sysBkInfoService.selectById(fkbkAcc.getBkId());

		SysFlowSHQueryCondition condition= new SysFlowSHQueryCondition();
		condition.setDataId(settleId);
		condition.setWordsInTheTable(FncPrjEaplySettleConstant.REF_FLOW_S_H_FPES);
		condition.setSysCd("11000");
		List<SysFlowSHDTO> list=this.flowApiService.searchFlowSHDTOList(condition);
		if(null!=list && list.size()==1){
			//this.fncCapPayeeInfoApiService.removeById(settleId);by gengchunlan 20190410
			SysFlowSHDTO sfDTO = list.get(0);
			FncCapPayeeInfoDTO capee= new FncCapPayeeInfoDTO();

			capee.setPayId(settleId);
			capee.setPrjId(prjId);

			//收款方
			capee.setBkId(esvo.getPayAcc());
			capee.setRcptUnt(skbkAcc.getAccNm());
			capee.setAccNo(skbkAcc.getAccNum());
			capee.setPayeeBkNm(skbkAcc.getBanAccNm());
			capee.setPayeeBkNo(skbkAcc.getBanAccNum());
			capee.setBkAccTypCd(skbkAcc.getPubVerPriCd());

			//付款方
			capee.setPayer(crsbjlist.get(fpbdto.getCrsbj()));//授信主体
			capee.setPayerAccNo(fkbkAcc.getAccNo());
			capee.setPayerBkNm(bkvo.getBkNm());
			capee.setPayerBkNo(bkvo.getBkNo());

			capee.setCrtUsrId(Long.parseLong(fpbdto.getCrtpsnecd()));
			capee.setCrtTm(new Date());

			capee.setPNo(sfDTO.getPNo());

			capee.setWishPayday(esvo.getPayTm());

			capee.setIsQuick(FncCapPayeeInfoConstant.FNC_PAYEE_NOQUICK);
			capee.setCorpCode(FncCapPayeeInfoConstant.FNC_PAYEE_CROP_CODE_ZJB);
			capee.setPayWayCd(FncCapPayeeInfoConstant.FNC_PAYEE_PAYWAY_CD);
			capee.setPayeeTypCd(FncCapPayeeInfoConstant.FNC_PAYEE_PAYEETYP_CD);
			capee.setIsDel(0);
			capee.setFundTypCd(FncCapPayeeInfoConstant.FNC_PAYEE_FUNDTYPE_CD);

			if(esvo.getCapital().compareTo(new BigDecimal(0))==1){
				capee.setId(Long.parseLong(settleId+"01"));
				capee.setPayAmt(Math.round(esvo.getCapital().doubleValue()*100));
				capee.setPurpCd("2");  // 1 利息 2本金  3 其他费用
				capee.setUseCd(fpbdto.getFncinsNM()+"提前结清本金");
				this.fncCapPayeeInfoApiService.add(capee);
			}
			if(esvo.getInterest().compareTo(new BigDecimal(0))==1){
				capee.setId(Long.parseLong(settleId+"02"));
				capee.setPurpCd("1");  // 1 利息 2本金  3 其他费用
				capee.setPayAmt(Math.round(esvo.getInterest().doubleValue()*100));
				capee.setUseCd(fpbdto.getFncinsNM()+"提前结清利息");
				this.fncCapPayeeInfoApiService.add(capee);
			}
			if(esvo.getOtherChar().compareTo(new BigDecimal(0))==1){
				capee.setPayAmt(Math.round(esvo.getOtherChar().doubleValue()*100));
				capee.setId(Long.parseLong(settleId+"03"));
				capee.setPurpCd("3");  // 1 利息 2本金  3 其他费用
				capee.setUseCd(fpbdto.getFncinsNM()+"提前结清其他费用");
				this.fncCapPayeeInfoApiService.add(capee);
			}
			if(esvo.getSetlleChar().compareTo(new BigDecimal(0))==1){
				capee.setPayAmt(Math.round(esvo.getSetlleChar().doubleValue()*100));
				capee.setId(Long.parseLong(settleId+"04"));
				capee.setPurpCd("3");  // 1 利息 2本金  3 其他费用
				capee.setUseCd(fpbdto.getFncinsNM()+"提前结清结清费用");
				this.fncCapPayeeInfoApiService.add(capee);
			}
		}
		/*
		 * FncPrjDetailDTO fncPrjDetailDTO = this.fncPrjFstpyInfoApiService.selectByFncPrjId(prjId);




		//condition.setBuOrgCd("0000");

		if(list.size()==1){








		 */
		//this.fncPrjPaySchService.s
		// TODO Auto-generated method stub

	}

}
